package com.dlc.beijingrfid.helper;

import android.support.annotation.NonNull;
import com.dlc.commonlibrary.utils.PrefUtil;
import com.dlc.beijingrfid.base.Constants;
import com.dlc.beijingrfid.net.bean.LoginBean;

/**
 * Created by John on 2017/6/23.
 */

public class UserHelper {

    private static UserHelper instance = new UserHelper();

    /**
     * 缓存在内存中的用户信息
     *
     * {
     * "code": 1,
     * "msg": "登录成功",
     * "data": {
     * "id": "1",//用户ID
     * "company_id": "0",//工厂名称
     * "account": "13800138000",//帐号名
     * "password": "96e79218965eb72c92a549dd5a330112",//密码（加密后）
     * "name": "黎#",//姓名
     * "sex": "1",//性别
     * "status": "1",//状态
     * "ctime": "1478541442",//创建时间
     * "lasttime": "1500879814",//最后登录时间
     * "lastip": "219.132.205.113",//最后登录IP
     * "logincount": "1338",//登录次数
     * "timestamp": 1500879983,//登录时间
     * "sign": "0d1b993c522532b147ed9ec305953e72",//签名
     * "nickname": "黎#"//昵称
     * }
     * }
     */
    public static class UserInfo {
        public int id;
        public String company_id;
        public String account;
        public String password;
        public String name;
        public int sex;
        public int status;
        public long ctime;
        public long lasttime;
        public String lastip;
        public int logincount;
        public String avatar;
        public String nickname;
        public int type;
        public String hotel_id;
        public long timestamp;
        public String sign;

        public void updateFrom(LoginBean loginBean) {
            if (loginBean.data == null) {
                return;
            }

            id = loginBean.data.id;
            company_id = loginBean.data.company_id;
            account = loginBean.data.account;
            password = loginBean.data.password;
            name = loginBean.data.name;
            sex = loginBean.data.sex;
            status = loginBean.data.status;
            ctime = loginBean.data.ctime;
            lasttime = loginBean.data.lasttime;
            lastip = loginBean.data.lastip;
            logincount = loginBean.data.logincount;
            timestamp = loginBean.data.timestamp;
            sign = loginBean.data.sign;
            nickname = loginBean.data.nickname;
            avatar = loginBean.data.avatar;
            // 新增的
            type = loginBean.data.type;
            hotel_id = loginBean.data.hotel_id;
        }

        public void reset() {
            id = -1;
            sex = -1;
            timestamp = -1;
            sign = null;
            type = -1;
        }
    }

    private UserInfo mUserInfo;

    private UserHelper() {
        mUserInfo = new UserInfo();
        mUserInfo.reset();
    }

    public static UserHelper get() {
        return instance;
    }

    public void saveUserInfo(LoginBean loginBean) {

        mUserInfo.updateFrom(loginBean);

        PrefUtil.getDefault()
            .putInt(Constants.USER.ID, mUserInfo.id)
            .putString(Constants.USER.COMPANY_ID, mUserInfo.company_id)
            .putString(Constants.USER.ACCOUNT, mUserInfo.account)
            .putString(Constants.USER.PASSWORD, mUserInfo.password)
            .putString(Constants.USER.NAME, mUserInfo.name)
            .putInt(Constants.USER.SEX, mUserInfo.sex)
            .putInt(Constants.USER.STATUS, mUserInfo.status)
            .putLong(Constants.USER.CTIME, mUserInfo.ctime)
            .putLong(Constants.USER.LASTTIME, mUserInfo.lasttime)
            .putString(Constants.USER.LASTIP, mUserInfo.lastip)
            .putLong(Constants.USER.LOGINCOUNT, mUserInfo.logincount)
            .putLong(Constants.USER.TIMESTAMP, mUserInfo.timestamp)
            .putString(Constants.USER.SIGN, mUserInfo.sign)
            .putString(Constants.USER.NICKNAME, mUserInfo.nickname)
            .putString(Constants.USER.AVATAR, mUserInfo.avatar)
            .putInt(Constants.USER.TYPE, mUserInfo.type)
            .putString(Constants.USER.HOTEL_ID, mUserInfo.hotel_id)
            .commit();
    }

    /**
     * 退出账号
     */
    public void logout() {
        // TODO: 2017/7/24  
    }

    public String getName() {
        if (mUserInfo.name == null) {
            mUserInfo.name = PrefUtil.getDefault().getString(Constants.USER.NAME, "");
        }
        return mUserInfo.name;
    }

    public void saveName(String name) {
        mUserInfo.name = returnNotNull(name);
        PrefUtil.getDefault().saveString(Constants.USER.NAME, name);
    }

    public String getNickname() {
        if (mUserInfo.nickname == null) {
            mUserInfo.nickname = PrefUtil.getDefault().getString(Constants.USER.NICKNAME, "");
        }
        return mUserInfo.nickname;
    }

    public void saveNickname(String nickname) {
        mUserInfo.nickname = nickname;
        PrefUtil.getDefault().saveString(Constants.USER.NICKNAME, nickname);
    }

    public void saveAccount(String account) {
        mUserInfo.account = account;
        PrefUtil.getDefault().saveString(Constants.USER.ACCOUNT, account);
    }

    public String getAccount() {
        if (mUserInfo.account == null) {
            mUserInfo.account = PrefUtil.getDefault().getString(Constants.USER.ACCOUNT, "");
        }
        return mUserInfo.account;
    }

    public void saveId(int id) {
        mUserInfo.id = id;
        PrefUtil.getDefault().saveInt(Constants.USER.ID, id);
    }

    public int getId() {
        if (mUserInfo.id == -1) {
            mUserInfo.id = PrefUtil.getDefault().getInt(Constants.USER.ID, -1);
        }
        return mUserInfo.id;
    }

    public long getTimestamp() {
        if (mUserInfo.timestamp == -1) {
            mUserInfo.timestamp = PrefUtil.getDefault().getLong(Constants.USER.TIMESTAMP, -1);
        }
        return mUserInfo.timestamp;
    }

    public void saveTimestamp(long timestamp) {
        mUserInfo.timestamp = timestamp;
        PrefUtil.getDefault().saveLong(Constants.USER.TIMESTAMP, timestamp);
    }

    public String getSign() {
        if (mUserInfo.sign == null) {
            mUserInfo.sign = PrefUtil.getDefault().getString(Constants.USER.SIGN, "");
        }
        return mUserInfo.sign;
    }

    public String getAvatar() {
        if (mUserInfo.avatar == null) {
            mUserInfo.avatar = PrefUtil.getDefault().getString(Constants.USER.AVATAR, "");
        }
        return mUserInfo.avatar;
    }

    public void saveAvatar(String avatar) {
        mUserInfo.avatar = returnNotNull(avatar);
        PrefUtil.getDefault().saveString(Constants.USER.AVATAR, avatar);
    }

    /**
     * 获取账户类型
     *
     * @return
     */
    public int getType() {
        if (mUserInfo.type == -1) {
            mUserInfo.type = PrefUtil.getDefault().getInt(Constants.USER.TYPE, -1);
        }
        return mUserInfo.type;
    }

    /**
     * 返回非null的字符串
     *
     * @param ori
     * @return
     */
    private static
    @NonNull
    String returnNotNull(String ori) {
        return ori == null ? "" : ori;
    }
}
